<?php

/**
 * XHTML_Col, xhtml_lib
 * 
 * This file contains the source code for the XHTML_Col class.
 * @author Fernando Jimenez Mocobono <fjmcool@gmail.com>
 * @version 0.1
 * @package xhtml_lib
 */

include_once "XHTML_Table_Common_Element.php";
include_once "XHTML__Table_Content.php";
include_once "XHTML__Col_Element.php";
include_once "XHTML__Block.php";
include_once "XHTML__Button_Content.php";

/**
 * The XHTML_Col class is a handler for the col xhtml-strict tag as a part 
 * of a xhtml-strict php-handler implementation.
 */
class XHTML_Table extends XHTML_Common_Element implements XHTML__Block, 
	XHTML__Button_Content {
	
	//inner elements

	/**
	 * Specifies a summary of the table for speech-synthesizing/non-visual 
	 * browsers.
	 * 
	 * @var string
	 */
	protected $summary;
	
	/**
	 * Specifies the border width.
	 *
	 * @var integer
	 */
	protected $border;
	
	/**
	 * Specifies which sides of the border surrounding a table will be visible.
	 * Valid values: void, above, below, hsides, lhs, rhs, vsides, box, border.
	 *
	 * @var string
	 */
	protected $frame;
	
	/**
	 * Specifies the horizontal/vertical divider lines.
	 * Valid values: none, groups, rows, cols, all.
	 *
	 * @var string
	 */
	protected $rules;
	
	/**
	 * Specifies the space between the cell walls and contents.
	 *
	 * @var number
	 */
	protected $cellSpacing;
	
	/**
	 * Specifies the space between cells.
	 *
	 * @var number
	 */
	protected $cellPadding;
	
	/**
	 * The table optional caption element.
	 * 
	 * @var XHTML_Caption
	 */
	protected $caption;
	
	/**
	 * The table optional col elements array.
	 *
	 * @var XHTML__Col_Element
	 */
	protected $colElements;
	
	/**
	 * The table optional thead element. (The header).
	 *
	 * @var XHTML_THead
	 */
	protected $head;
	
	/**
	 * The table optional tfoot element. (The footer).
	 *
	 * @var XHTML_TFoot
	 */
	protected $foot;
	
	/**
	 * The table body elements array. (tbody | tr).
	 *
	 * @var XHTML__Table_Content
	 */
	protected $bodyElements;
	
	/**
	 * Construct a table object with at least one body element.
	 *
	 * @param XHTML__Table_Content $bodyElement
	 */
	function __construct(XHTML__Table_Content $bodyElement)
	{
		settype($this->bodyElements, "array");
		settype($this->colElements, "array");
		$this->bodyElements[] = $bodyElement;
	}
	
	/**
	 * Sets a summary of the table for speech-synthesizing/non-visual 
	 * browsers.
	 * 
	 * @param string $summary 
	 */
	function setSummary($summary)
	{
		$this->summary = $summary;
	}
	
	/**
	 * Gets the summary of the table for speech-synthesizing/non-visual 
	 * browsers. (If set previously).
	 *
	 * @return string
	 */
	function getSummary()
	{
		return $this->summary;
	}
	
	/**
	 * Sets the border width.
	 *
	 * @param integer $border
	 */
	function setBorder($border)
	{
		$this->border = $border;
	}
	
	/**
	 * Gets the border width.
	 *
	 * @return integer
	 */
	function getBorder()
	{
		return $this->border;
	}
	
	/**
	 * Sets which sides of the border surrounding a table will be visible.
	 * Valid values: void, above, below, hsides, lhs, rhs, vsides, box, border.
	 *
	 * @param string $frame
	 */
	function setFrame($frame)
	{
		$this->frame = $frame;
	}
	
	/**
	 * Gets which sides of the border surrounding a table will be visible.
	 * Valid values: void, above, below, hsides, lhs, rhs, vsides, box, border.
	 *
	 * @return string
	 */
	function getFrame()
	{
		return $this->frame;
	}
	
	/**
	 * Sets the horizontal/vertical divider lines.
	 * Valid values: none, groups, rows, cols, all.
	 *
	 * @param string $rules
	 */
	function setRules($rules)
	{
		$this->rules = $rules;
	}
	
	/**
	 * Gets the horizontal/vertical divider lines.
	 * Valid values: none, groups, rows, cols, all.
	 *
	 * @return unknown
	 */
	function getRules()
	{
		return $this->rules;
	}
	
	/**
	 * Sets the space between the cell walls and contents.
	 *
	 * @param string $cellSpacing
	 */
	function setCellSpacing($cellSpacing)
	{
		$this->cellSpacing = $cellSpacing;
	}
	
	/**
	 * Gets the space between the cell walls and contents.
	 * 
	 * @return string
	 */
	function getCellSpacing()
	{
		return $this->cellSpacing;
	}
	
	/**
	 * Sets the space between cells.
	 *
	 * @param string $cellPadding
	 */
	function setCellPadding($cellPadding)
	{
		$this->cellPadding = $cellPadding;
	}
	
	/**
	 * Gets the space between cells.
	 *
	 * @return string
	 */
	function getCellPadding()
	{
		return $this->cellPadding;
	}
	
	/**
	 * Sets the table optional caption element.
	 *
	 * @param XHTML_Caption $caption
	 */
	function setCaption(XHTML_Caption $caption)
	{
		$this->caption = $caption;
	}
	
	/**
	 * Gets the table optional caption element.
	 * 
	 * @return XHTML_Caption
	 */
	function getCaption()
	{
		return $this->caption;
	}
	
	/**
	 * Sets the table optional col elements array.
	 *
	 * @param array $colElements
	 */
	function setColElements($colElements)
	{
		$this->colElements = $colElements;
	}
	
	/**
	 * Gets the table optional col elements array.
	 *
	 * @return array
	 */
	function getColElements()
	{
		return $this->colElements;
	}
	
	/**
	 * Adds an element to the inner col element array.
	 *
	 * @param XHTML__Col_Element $element
	 */
	function addColElement(XHTML__Col_Element $element)
	{
		$this->colElements[] = $element;
	}
	
	/**
	 * Remove the given element from the inner col element array.
	 *
	 * @param XHTML__Col_Element $element
	 */
	function removeColElement(XHTML__Col_Element $element)
	{
		if (($res = array_search($element, $this->colElements))) {
			unset($this->colElements[$res]);
		}
	}
	
	/**
	 * Sets the table optional thead element. (The header).
	 *
	 * @param XHTML_THead $head
	 */
	function setHead(XHTML_THead $head)
	{
		$this->head = $head;
	}
	
	/**
	 * Gets the table optional thead element. (The header).
	 * 
	 * @return XHTML_THead
	 */
	function getHead()
	{
		return $this->head;
	}
	
	/**
	 * Sets the table optional tfoot element. (The footer).
	 *
	 * @param XHTML_TFoot $foot
	 */
	function setFoot(XHTML_TFoot $foot)
	{
		$this->foot = $foot;
	}
	
	/**
	 * Gets the table optional tfoot element. (The footer).
	 *
	 * @return XHTML_TFoot
	 */
	function getFoot()
	{
		return $this->foot;
	}
	
	/**
	 * Sets the table body elements array.
	 *
	 * @param array $bodyElements
	 */
	function setBodyElements($bodyElements)
	{
		$this->bodyElements = $bodyElements;
	}
	
	/**
	 * Gets the table body elements array.
	 * 
	 * @return array
	 */
	function getBodyElements()
	{
		return $this->bodyElements;
	}
	
	/**
	 * Adds an element to the inner body elements array. (tbody | tr).
	 *
	 * @param XHTML__Table_Content $element
	 */
	function addBodyElement(XHTML__Table_Content $element)
	{
		$this->bodyElements[] = $element;
	}
	
	/**
	 * Removes the given element from the inner body elements array.
	 * 
	 * @param XHTML__Table_Content $element
	 */
	function removeBodyElement(XHTML__Table_Content $element)
	{
		if (($res = array_search($element, $this->bodyElements))) {
			unset($this->bodyElements[$res]);
		}
	}
	
	// Overriding the toString function for a proper implementation.
	
	function toString()
	{
		$auxString = "<table".parent::toString();
		
		if (isset($this->summary)) {
			$auxString = $auxString."summary=\"".$this->summary."\" ";
		}
		
		if (isset($this->border)) {
			$auxString = $auxString."border=\"".$this->border."\" ";
		}
		
		if (isset($this->frame)) {
			$auxString = $auxString."frame=\"".$this->frame."\" ";
		}
		
		if (isset($this->rules)) {
			$auxString = $auxString."rules=\"".$this->rules."\" ";
		}
		
		if (isset($this->cellSpacing)) {
			$auxString = $auxString."cellspacing=\"".$this->cellSpacing."\" ";
		}
		
		if (isset($this->cellPadding)) {
			$auxString = $auxString."cellpadding=\"".$this->cellPadding."\" ";
		}
		
		$auxString = $auxString.">\n";
		
		if (isset($this->caption)) {
			$auxString = $auxString.$this->caption->toString();
		}
		
		if (count($this->colElements) > 0) {
            foreach ($this->colElements as $colElement) {
                $auxString = $auxString.$colElement->toString();
            }
        }
        
        if (isset($this->head)) {
        	$auxString = $auxString.$this->head->toString();
        }
        
        if (isset($this->foot)) {
        	$auxString = $auxString.$this->foot->toString();
        }
        
		if (count($this->bodyElements) > 0) {
            foreach ($this->bodyElements as $bodyElement) {
                $auxString = $auxString.$bodyElement->toString();
            }
        }
		
		$auxString = $auxString."</table>\n";
		
		return $auxString;
	}
	
}

?>